import Vue from 'vue'
import VueRouter from 'vue-router'

import Login from '../views/Login.vue'
import Home from '../components/Home.vue' // 导入单页面
import Welcome from '../views/Welcome.vue' // 导入欢迎页面
import Users from '../components/user/Users' // 导入用户管理页面
import Rights from '../components/power/rights.vue' // 导入权限管理
import Roles from '../components/power/roles.vue' // 导入角色管理
import Cate from '../components/goods/cate.vue' //
import Type from '../components/goods/params' // vue，js 扩展名可省略
import Goods from '@/components/goods/goods.vue' // 商品列表
import AddGood from '@/components/goods/AddGood' // 增加商品
import order from '@/views/order/order'
import report1 from '@/views/report/report1'
import product from '@/views/product/product'
import inku from '@/views/Inku/inku' // 入库单
import InproductList from '@/views/Inku/InProducList' // 进货单列表
Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', name: 'login', component: Login },
  {
    path: '/home',
    name: 'home',
    redirect: '/welcome',
    component: Home,
    children: [
      { path: '/welcome', name: 'welcome', component: Welcome },
      { path: '/users', name: 'users', component: Users }, // 用户管理
      { path: '/permission', name: 'permission', component: Rights }, // 权限
      { path: '/Roles', name: 'permission2', component: Roles }, // 角色管理
      { path: '/cate', name: 'cate', component: Cate }, // 类别管理
      { path: '/type', name: 'type', component: Type }, // 参数管理
      { path: '/Goods', name: 'goods', component: Goods }, // 商品管理
      { path: '/AddGood', name: 'addgood', component: AddGood }, // 商品增加
      { path: '/order', name: 'order', component: order }, // 商品增加
      { path: '/report1', name: 'report1', component: report1 }, // 图表1
      { path: '/product', name: 'product', component: product }, // SKU货品
      { path: '/inku/:gid', name: 'inku', component: inku, props: true }, // 入库
      { path: '/InproductList', name: 'InproductList', component: InproductList }
    ]
  } // 配置子路由导航

]

const router = new VueRouter({
  routes
})
// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
  // to 将要访问的路径
  // from 代表从哪个路径跳转而来
  // next 是一个函数，表示放行
  //     next()  放行    next('/login')  强制跳转

  if (to.path === '/login') return next() // 访问登陆页，直接放行
  // 获取token
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) {
    window.alert('暂末登陆，不能访问')
    return next('/login') // 说明没有登陆，没有登陆去 登陆
  }
  next() // 放行
})

export default router
